app.controller('BusShiftTplListCtrl', function ($scope, ajax, $modal, toaster) {

	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.routeList = [];
	//状态
	$scope.statusList = [
		{ id: 1, name: "正常" },
		{ id: 2, name: "暂停" },
		{ id: 3, name: "停用" }
	];

	$scope.reload = $scope.page.loader('shift/bs/tpl/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.query = function () {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.routeList.selected != undefined) {
			$scope.queryMap.route__id = $scope.routeList.selected.id;
		} else if ($scope.routeList.selected == undefined) {
			delete $scope.queryMap.route__id;
		}
		$scope.reload(1, $scope.queryMap);
	};
	$scope.query();

	//获取线路列表
	$scope.loadRouteList = function () {
		ajax.call(new Req.GET({ resource: 'route/bs/list' })).then(function (result) {
			$scope.routeList = result.result;
		}, function () { });
	};
	$scope.loadRouteList();


	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.unsync = function (shiftNo, days) {
		if (days && days.length > 0) {
			console.log(days);
			$modal.open({
				templateUrl: 'template/bus/shiftTemplate/unsyncModal.html?v=3ab9233bf1',
				controller: 'ModalCtrl',
				size: 'md',
				resolve: {
					param: function () {
						return {
							"days": days,
							"shiftNo": shiftNo
						};
					}
				}
			});
		}
	};
	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		var updateModal = {
			templateUrl: 'template/bus/shiftTemplate/writeModal.html?v=8903c21669',
			controller: 'BusShiftTplModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		};
		$modal.open(updateModal).result.then(function (param) {
			if (param.type == 'update') {
				if (param.ids) updater.datalist = param.ids;
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'shift/bs/tpl/update';
				ajax.call(updater).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '修改信息成功。');
						$scope.unsync(param.data.shiftNo, result.result);
						$scope.reload($scope.bigCurrentPage, $scope.queryMap);
					} else if (result.code != 423) {
						toaster.pop('error', '错误', result.result);
					}
				}, function () {
					toaster.pop('error', '连接错误', '与服务器的连接失败！');
				});
			} else if (param.type == 'insert') {
				inserter.fields = param.data;
				inserter.resource = 'shift/bs/tpl/insert';
				$scope.page.add(inserter, $scope.reload, $scope.queryMap);
			}
		});
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		$scope.delModal(size, current_data, submit_type).result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'shift/bs/tpl/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload);
			}
		});
	};

	$scope.updateStation = function (tpl) {
		$scope.current_data = angular.copy(tpl);
		$modal.open({
			templateUrl: 'template/bus/shiftTemplate/updateStation.html?v=7277ee56b4',
			controller: 'BusShiftTplStaionModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": "update"
					};
				}
			}
		}).result.then(function () {
			$scope.reload($scope.bigCurrentPage, $scope.queryMap);
		});
	};

	$scope.import = function () {
		$modal.open({
			templateUrl: 'template/bus/shiftTemplate/import.html?v=bf486c9936',
			controller: 'TemplateImportCtrl',
			size: 'lg',
			resolve: {
				param: function () { return {}; }
			}
		}).result.then(function (param) {
			ajax.call(new Req.POST({
				fields: { filePath: param },
				resource: 'shift/bs/bulkimport'
			})).then(function (result) {
				if (result && result.code == 200) {
					toaster.pop('success', '成功', result.result);
				} else if (result.message == null) {
					toaster.pop('error', '错误', '找不到文件路径！');
				} else {
					toaster.pop('error', '错误', result.message);
				}
			}, function () {
				toaster.pop('error', '错误', '数据导入失败，连接错误！');
			});
		});
	};

	$scope.calendar = function (tpl) {
		$scope.current_data = angular.copy(tpl);
		ajax.call(new Req.GET({
			resource: 'shift/bs/' + tpl.id + '/all'
		})).then(function (result) {
			$scope.dateList = result.result;

			$scope.events = [];
			for (var i = 0; i < $scope.dateList.length; i++) {
				var day = {};
				day.title = "✔"; //设定具体某一天的日程标题
				day.start = new Date($scope.dateList[i].departDate); //设定这个日程的开始时间
				day.allDay = true;  //设定这个日程是否为全天 默认false
				day.textColor = "red";
				$scope.events.push(day);
			}

			$modal.open({
				templateUrl: 'template/bus/shiftTemplate/dateList.html?v=72ef886541',
				controller: 'BusShiftTplDateListCtrl',
				size: 'lg',
				resolve: {
					deps: ['$ocLazyLoad', 'uiLoad',
						function ($ocLazyLoad, uiLoad) {
							return uiLoad.load([
								'vendor/jquery/fullcalendar/fullcalendar.css',
								'vendor/jquery/fullcalendar/theme.css',
								'vendor/jquery/jquery-ui-1.10.3.custom.min.js',
								'vendor/libs/moment.min.js',
								'vendor/jquery/fullcalendar/fullcalendar.min.js'
							]).then(function () {
								return $ocLazyLoad.load('ui.calendar');
							});
						}],
					param: function () {
						return {
							"data": $scope.current_data,
							"list": $scope.events
						};
					}
				}
			});
		}, function () { });
	};

	// 关联汽车票
	$scope.attach = function (tpl) {
		$scope.current_data = angular.copy(tpl);
		$modal.open({
			templateUrl: 'template/bus/shiftTemplate/attachShift.html?v=31757c8348',
			controller: 'BusShiftTplAttachCtrl',
			size: 'md',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
					};
				}
			}
		});
	};

});
